package com.avic.rong.fragment.course;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.avic.rong.R;
import com.avic.rong.course.activity.PeiXunVideoActivity;
import com.avic.rong.course.adapter.PeiXunCourseVideoRvAdapter;
import com.avic.rong.course.bean.PeiXunListBean;
import com.avic.rong.course.showapi.PeiXunListApi;
import com.avic.rong.fragment.BaseLazyFragment;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.orhanobut.logger.Logger;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页-关注
 */
@SuppressLint("ValidFragment")
public class FragmentVideoCourse extends BaseLazyFragment implements OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.loadFollow)
    LoadingLayout loading_layout;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private Context mContext;
    private PeiXunListApi classMuLuApi;
    private String courseClassfyId = ConstantsUtils.EMPTY_STR;
    private String mTitle;
    private PeiXunCourseVideoRvAdapter videoAdapter;
    private ArrayList<PeiXunListBean.DataBean> list;
    public static FragmentVideoCourse getInstance(String title, Context context, String courseClassfyId) {
        FragmentVideoCourse sf = new FragmentVideoCourse();
        sf.mContext = context;
        sf.mTitle = title;
        sf.courseClassfyId = courseClassfyId;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_video_course, null);
        ButterKnife.bind(this, v);
        list = new ArrayList<>();
        videoAdapter = new PeiXunCourseVideoRvAdapter(mContext,list);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(videoAdapter);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setOnRefreshListener(this);
        loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                currentPageNumber = 1;
                if (classMuLuApi != null){
                    HttpManager.getInstance().doHttpDeal(classMuLuApi);
                }
            }

            @Override
            public void onReturn(View v) {

            }
        });


        return v;
    }

    @Override
    public void loadData() {
        Logger.i("--------------------rr-------" + "0:" + courseClassfyId);
        classMuLuApi = new PeiXunListApi((PeiXunVideoActivity) mContext, simpleOnNextListener, String.valueOf(10), courseClassfyId+":0"  , 10, "zh:");
        classMuLuApi.setPageNo(currentPageNumber);
        classMuLuApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);
    }


    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(mContext, subjects.getMsg());
                Logger.e("cjx", subjects.getMsg());
                return;
            }
            PeiXunListBean bean = (PeiXunListBean) subjects.getResult();

            if (isRefresh) {
                list.clear();
            }
            list.addAll(bean.getData());
            videoAdapter.notifyDataSetChanged();

            if (videoAdapter.getList() == null || videoAdapter.getList().size() <= 0) {
                loading_layout.setStatus(LoadingLayout.EMPTY);
            } else {
                loading_layout.setStatus(LoadingLayout.SUCCESS);
            }

            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }

        }

        @Override
        public void onError(final Throwable e) {
            if (isRefresh) {
                refreshLayout.finishRefresh();
            } else {
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.ERROR);
            Logger.e("cjx", "培训列表数据：" + e.getMessage());
        }
    };

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        classMuLuApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {

        isRefresh = true;
        currentPageNumber = 1;
        classMuLuApi.setPageNo(currentPageNumber);
        HttpManager.getInstance().doHttpDeal(classMuLuApi);

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    @Override
    public void stopData() {
        super.stopData();
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                if (LoadingLayout.SUCCESS != loading_layout.getStatus()) {
                    loading_layout.setStatus(LoadingLayout.SUCCESS);
                }
            }
        });
    }
}